package com.wlyuan.index.domain.index.valueobj;

import com.wlyuan.core.marker.INamed;
import com.wlyuan.core.marker.NamedMap;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.var;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

;

/**
 * @author yuanjie
 */
@RequiredArgsConstructor
@Getter
@Deprecated
public enum QuotationStatus implements INamed<Integer> {
    DRAFT(Status.DRAFT, Status.DRAFT, Status.DRAFT, "草稿"),
    SUBMITTED(Status.SUBMITTED, Status.SUBMITTED, Status.AUDITING, "已发送"),
    @Deprecated // FIXME: 这个状态实际不存在，但需要调整报价系统，先兼容
    AUDITING(Status.AUDITING, Status.SUBMITTED, Status.AUDITING, "待审核"),
    AGREED(Status.AGREED, Status.AGREED, Status.AGREED, "已同意"),
    REJECTED(Status.REJECTED, Status.REJECTED, Status.REJECTED, "已拒绝"),
    BARGAIN(Status.BARGAIN, Status.BARGAIN, Status.BARGAINING, "议价"),
    RECALLED(Status.RECALLED, Status.RECALLED, Status.RECALLED, "已撤回"),
    RETURNED(Status.RETURNED, Status.RETURNED, Status.RETURNED, "已退回"),
    DISCARDED(Status.DISCARDED, Status.DISCARDED, Status.DISCARDED, "已作废"),
    CLOSED(Status.CLOSED, Status.CLOSED, Status.CLOSED, "已关闭");

    private final Integer value;
    private final Integer starter;
    private final Integer reviewer;
    private final String name;


    private interface Status {
        Integer DRAFT = 0;
        Integer SUBMITTED = 1;
        Integer AGREED = 2;
        Integer REJECTED = 3;
        Integer BARGAIN = 8;
        Integer AUDITING = 11;
        Integer RECALLED = 12;
        Integer RETURNED = 13;
        Integer DISCARDED = 14;
        Integer BARGAINING = 18;
        Integer CLOSED = 32;
    }

    public int getPriority() {
        if (LOWERS.contains(this)) {
            return -this.value;
        }
        return this.value;
    }


    private static final List<QuotationStatus> LOWERS = Arrays.asList(RECALLED, RETURNED, DISCARDED);
    private static final List<QuotationStatus> CAN_QUOTE = Arrays.asList(RECALLED, RETURNED, DISCARDED);
    private static final Map<Integer, QuotationStatus> STATUSES = new NamedMap<>(QuotationStatus.class);

    public static QuotationStatus valueOf(Integer value) {
        return STATUSES.get(value);
    }

    public static boolean canQuote(Integer value) {
        if (null == value) {
            return true;
        }
        var status = valueOf(value);
        return CAN_QUOTE.contains(status);
    }

    public boolean isCanQuote() {
        return QuotationStatus.canQuote(this.getValue());
    }
}